package com.luck.easyfoodbatch.mvp.cart;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.CartM;
import com.luck.easyfoodbatch.mvp.model.ModifyNum;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.cart
 * email：893855882@qq.com
 * data：16/11/11
 */
public class ModifyCartNumPresenter implements ModifyCartNumModel {
    private final String TAG = getClass().getSimpleName();
    private ModifyCartNumView modifyCartNumView;
    private Context mContext;

    public ModifyCartNumPresenter(ModifyCartNumView modifyCartNumView, Context mContext) {
        this.modifyCartNumView = modifyCartNumView;
        this.mContext = mContext;
    }


    @Override
    public void requestModifyCart(String cid, int num, String province_id) {
        HttpParams params = new HttpParams();
        params.put("cid", cid);
        params.put("num", num + "");
        params.put("province_id", province_id);
        CacheMode mode = CacheMode.DEFAULT;
        OkHttpUtils.post(HttpUtil.MODIFY_CART_NUM)
                .tag(this)
                .cacheMode(mode)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, ModifyNum.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        modifyCartNumView.modifyCartNumSuccess((ModifyNum) data);
                        modifyCartNumView.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        modifyCartNumView.hideLoading();
                        modifyCartNumView.httpStatusError();
                    }
                });
    }
}
